<template>
    <div id="app">
        <el-card shadow="always" style="width:95%">
            <!-- 1. 分页条件-->
            <el-form :inline="true" :model="studentSearch" class="demo-form-inline" style="text-align: left;" size="medium">
                <el-form-item>
                    <el-input v-model="studentSearch.sname" placeholder="学生姓名"/>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="studentSearch.addr" placeholder="学生住址"/>
                </el-form-item>
                <el-form-item label="所在班级">
                    <el-select v-model="studentSearch.cid" placeholder="所在班级">
                        <el-option label="所有班级" value="0"></el-option>
                        <el-option :label="c.cname" :value="c.cid" v-for="c in classes" :key="c.cid"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search" plain icon="el-icon-search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="success" @click="addStudent" plain icon="el-icon-plus">添加学生</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="warning" @click="handleDelete" icon="el-icon-delete" plain >批量删除</el-button>
                </el-form-item>
            </el-form>
            <el-table
                    :data="students"
                    @selection-change="handleSelectionChange"
                    style="width: 100%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        label="学号"
                        prop="sid"
                        align="center"
                        width="180"/>
                <el-table-column
                        label="姓名"
                        align="center"
                        prop="sname"
                />
                <el-table-column
                        label="性别"
                        align="center"
                        prop="sex"
                />
                <el-table-column
                        label="年龄"
                        align="center"
                        prop="age"
                />
                <el-table-column
                        label="住址"
                        align="center"
                        prop="addr"
                />
                <el-table-column
                        label="所在班级"
                        align="center"
                        prop="cname"/>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button-group>
                        <el-button
                                size="mini"
                                type="primary"
                                round
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)">
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                round
                                icon="el-icon-delete"
                                @click="handleDelete(scope.$index, scope.row)">
                        </el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </el-table>
            <!--  分页导航-->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="page"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
            <!--  编辑学生的对话框  -->
            <el-dialog :title="student.sid ? '修改学生' : '添加学生'" :visible.sync="dialogFormVisible" 
                       :close-on-click-modal="false" style="text-align: left;">
                {{student}}
                <el-form ref="form" :model="student" label-width="100px">
                    <el-form-item label="学生姓名">
                        <el-input v-model="student.sname"></el-input>
                    </el-form-item>
                    <el-form-item label="学生性别">
                        <el-radio v-model="student.sex" label="男">男</el-radio>
                        <el-radio v-model="student.sex" label="女">女</el-radio>
                    </el-form-item>
                    <el-form-item label="学生年龄">
                        <el-input v-model="student.age"></el-input>
                    </el-form-item>
                    <el-form-item label="学生住址">
                        <el-input v-model="student.addr"></el-input>
                    </el-form-item>

                    <el-form-item label="所在班级">
                        <el-select v-model="student.cid" placeholder="请选择所在班级">
                            <el-option :label="c.cname" :value="c.cid" v-for="c in classes" :key="c.cid"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="save">确 定</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                students: [],
                page: 1,       // 当前页
                pageSize: 5,  // 每页大小
                total: 100,   // 总记录数
                studentSearch: {},   // 查找条件对象
                classes: [],   // 所有班级列表
                dialogFormVisible: false, // 隐藏对话框
                student:{},    // 当前正在编辑的学生对象
                multipleSelection:[],   // 用户选择的学生列表
                sids:[],                // 要删除的学和编号数组
            }
        },
        created() {
            // this.findAllStudents()
            this.search()
            this.findClasses()
        },
        methods: {
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(this.multipleSelection)
            },
            // 添加学生
            addStudent(){
                this.student = {}
                this.dialogFormVisible = true
            },
            //1. 查询所有班级列表
            findClasses() {
                this.axios.get("/classes").then(({data}) => {
                    console.log("classes:", data)
                    this.classes = data.data
                })
            },
            // 每页大小改变时触发
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val
                this.search()
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.page = val
                this.search()
            },
            //1. 查询所有学生
            findAllStudents() {
                this.axios.get("/student?cmd=list").then(({data}) => {
                    this.students = data.data
                })
            },
            //2. 条件查询带分页
            search() {
                this.axios.post(`/student?cmd=search&page=${this.page}&pageSize=${this.pageSize}`,
                    this.studentSearch).then(({data}) => {
                    console.log("data:(search)", data)
                    this.students = data.data.rows
                    this.total = data.data.total
                })
            },
            //3. 保存学生 (添加或修改)
            save(){
                //3.1 确定url地址
                let url = "/student?cmd=add"
                if(this.student.sid){
                    url = "/student?cmd=update"
                }
                //3.2 向后台发送请求
                this.axios.post(url,this.student).then(({data}) => {
                    console.log("data:",data)
                    let _this = this
                    if(data.code === 200){
                        this.$message({
                            showClose: true,
                            message: data.msg,
                            type: 'success',
                            onClose(){
                                _this.dialogFormVisible = false
                                _this.search()
                            }
                        });
                    }
                })
            },
            handleEdit(index, row) {
                console.log(index, row);
                //1. 为当前显示在对话框中的学生对象赋值
                this.student = row
                //2. 打开对话框
                this.dialogFormVisible = true
            },
            // 删除学生
            handleDelete(index, row) {
                //问题，如果区分是批量删除还是单个删除，可以根据row是否有值来判断，如果有值就是单个删除？
                if(row){        // 单选
                    this.sids = [row.sid]
                }else{          // 多选
                    this.sids = this.multipleSelection.map(m => m.sid)
                }
                this.$confirm(`是否真的要删除学号为：${this.sids}的学生？`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {         // 代表点了‘确定’按钮后执行的代码
                    //1. 向后台发送删除请求
                    this.axios.get("/student?cmd=delete&sids="+this.sids).then(({data}) => {
                        const _this = this
                        if(data.code === 200){
                            this.$message({
                                type: 'success',
                                message: data.msg,
                                onClose(){          // 关闭消息框时，刷新页面
                                    _this.search()
                                }
                            });
                        }
                    })

                }).catch(() => {        // 代表点了‘取消’按钮后执行的代码
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
        }
    }
</script>

<style>
    .el-pagination {
        text-align: right;
        margin-top: 20px;
    }

    .el-card {
        margin: 0 auto;
    }

    #app {
        font-family: Avenir, Helvetica, Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        text-align: center;
        color: #2c3e50;
    }

    nav {
        padding: 30px;
    }

    nav a {
        font-weight: bold;
        color: #2c3e50;
    }

    nav a.router-link-exact-active {
        color: #42b983;
    }
</style>
